package Long.Lu.Controller;


import Long.Lu.Service.RecruitService;
import Long.Lu.pojo.RecruitManager;
import Long.Lu.pojo.RecruitParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/recruit")
public class RecruitManagerController {

    @Autowired
    private RecruitService recruitService;



    //图片服务器路径
    @Value("${file.address}")
    private String serviceUrl;

    //图片访问路径
    @Value("${file.staticPath}")
    private String filePath;

    @RequestMapping("/listRecruit")
    public String listRecruit(Model model){
        List<RecruitManager> list = recruitService.select();
        //存值
        model.addAttribute("list",list);
        System.out.println(list);

        return "recruit/listRecruit";
    }

    @RequestMapping("/zhaoPin")
    public String zhaoPin(){

        return "/recruit/zhaoPin";
    }

    @RequestMapping("/updateState")
    @ResponseBody
    public boolean updateState(@RequestParam("rId") Integer rId){
        return recruitService.update(rId);
    }

    @RequestMapping("/recruitAdd")
    public String recruitAdd(){

        return "/recruit/recruitAdd";
    }


    //上传文件照片
    @RequestMapping("/upload")
    @ResponseBody
    public HashMap<String,Object> upload(MultipartFile file){
        HashMap<String,Object> map = new HashMap<String,Object>();
        try {
            //获取文件后缀
            String fileName = file.getOriginalFilename();
            String oName = fileName.substring(fileName.lastIndexOf(".")+1);
            //时间目录
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String timeDf = sdf.format(new Date());
            //保证文件名的唯一性
            String fName = UUID.randomUUID()+"";

            //拼接上传吧文件的全路径
            String uploadName = serviceUrl+"//"+timeDf+"//"+fName+"."+oName;

            //创建文件对象
            File f = new File(uploadName);

            if(!f.isDirectory()){
                f.mkdirs();
            }

            //保存文件
            file.transferTo(f);

            //吧图片路径传到前端
            map.put("src",filePath+"/"+fName+"."+oName);

            //成功
            map.put("code",0);

            return map;
        } catch (IOException e) {
            e.printStackTrace();
        }
        //失败提示
        map.put("src","");
        map.put("code",1);

        // File e = new File("d:")
        return map;
    }

    //
    @RequestMapping("/save/{path}")
    @ResponseBody
    public boolean add(RecruitParam recruitParam, @PathVariable("path") String path){

        if (path.equals("add")) {
            //新增方法
            return recruitService.add(recruitParam);

        }else{
            //删除方法

        }
        return false;

    }

    //删除
    @RequestMapping("/deletes")
    @ResponseBody
    public boolean deletes(@RequestParam("rId") Integer rId){
        recruitService.deletes(rId);
        return true;
    }

    //批量删除
    @RequestMapping("/delAlls")
    @ResponseBody
    public boolean delAlls( String rIds){

        return recruitService.deletess(rIds);
    }

}
